package com.example.zg3_park.CarType

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.example.lib_base.entity.AddParkEntity
import com.example.lib_base.entity.CarTypeEntity
import com.example.lib_base.entity.CarTypeEntityItem
import com.example.lib_base.mvvm.BaseViewModel
import com.example.lib_base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow

import javax.inject.Inject

@HiltViewModel
class CarTypeViewModel @Inject constructor(val apiService:ApiService):BaseViewModel() {

    //添加
    val addParkData = MutableLiveData<AddParkEntity>()
    //修改
    val XiuParkData = MutableLiveData<CarTypeEntity>()
    fun getCarType(para:String,type:String): Flow<PagingData<CarTypeEntityItem>> {
        return Pager(config = PagingConfig(10), pagingSourceFactory = {
            CarTypeDataSource(apiService,para,type)
        }).flow.cachedIn(viewModelScope)
    }

    fun addPark(item:AddParkEntity) {
        httpRequest({apiService.addPark(toBody(item))},addParkData::postValue)
    }

    fun XiuPark(id:Int,item:CarTypeEntity) {
        httpRequest({apiService.XiuPark(id,toBody(item))},XiuParkData::postValue)
    }
}